//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    loadingHidden: false, // loading
    userInfo: {},
    swiperCurrent: 0,
    selectCurrent: 0,
    categories: [],
    activeCategoryId: 0,
    goods: [],
    scrollTop: 0,
    loadingMoreHidden: true,
    activited:true,
    hasNoCoupons: true,
    coupons: [],
    searchInput: '',
    brandDes: [{
      url: 'amplifi.png',
      id: 0,
      title: 'amplifi',
      des: '电通安吉斯集团中国重要的媒体投资业务部门'
    },{
      url: 'baidu.png',
      id: 1,
      title: '百度',
      des: '全球最大的中文搜索引擎'
    },
    {
      url: 'baofeng.png',
      id: 3,
      title: '暴风',
      des: '暴风影音致力打造大型互联网视频播放平台,集在线视频和本地播放服务于一体,是专注提供免费、高清网络视频服务的大型视频网站。'
    }, {
      url: 'fenghuang.png',
      id: 2,
      title: '凤凰网',
      des: '电通安吉斯集团中国重要的媒体投资业务部门'
    }, {
      url: 'fengxing.png',
      id: 3,
      title: '风行网',
      des: '新一代视频风行网,提供免费电影、电视剧、综艺、动漫、体育等视频内容的在线观看和下载。'
    }, {
      url: 'google.png',
      id: 4,
      title: 'GOOGLE',
      des: '公认为全球最大的搜索引擎公司'
    }, {
      url: 'iflytek.png',
      id: 4,
      title: '科大讯飞',
      des: '专业从事智能语音及语言技术研究、软件及芯片产品开发、语音信息服务及电子政务系统集成'
    }, {
      url: 'iqiyi.png',
      id: 3,
      title: '爱奇艺',
      des: '技术开发、技术咨询、技术服务、技术转让；设计、制作、代理、发布广告。'
    }, {
      url: 'letv.png',
      id: 1,
      title: '乐视',
      des: '乐视超级手机,超级电视,乐视网会员官方销售平台'
    }, {
      url: 'mgtv.png',
      id: 2,
      title: '芒果TV',
      des: '芒果TV是湖南广播电视台旗下唯一互联网视频平台'
    }, {
      url: 'miaozhen.png',
      id: 1,
      title: '秒针',
      des: '一家应用云计算、云存储和人工智能技术对数字化广告进行评估的创新型企业'
    }, {
      url: 'sohu.png',
      id: 1,
      title: '搜狐',
      des: '为用户提供24小时不间断的最新资讯,及搜索、邮件等网络服务'
    }, {
      url: 'tencent.png',
      id: 3,
      title: '腾讯视频',
      des: '致力于打造中国领先的在线视频媒体平台'
    }, {
      url: 'toutiao.png',
      id: 1,
      title: '今日头条',
      des: '一款基于数据挖掘的推荐引擎产品,它为用户推荐有价值'
    }, {
      url: 'weibo.png',
      id: 2,
      title: '微博',
      des: '随时随地发现新鲜事！微博带你欣赏世界上每一个精彩瞬间'
    }, {
      url: 'xiaomi.png',
      id: 1,
      title: '小米平台',
      des: '一家专注于高端智能手机、互联网电视以及智能家居生态链建设的创新型科技企业。'
    }, {
      url: 'youku.png',
      id: 3,
      title: '优酷',
      des: '阿里巴巴文化娱乐集团大优酷事业群下的视频平台。'
    }]
  },

  tabClick: function (e) {
    this.setData({
      activeCategoryId: e.currentTarget.id
    });
    this.getGoodsList(this.data.activeCategoryId);
  },
  changeClick: function (e) {
    this.setData({
      activited: !this.data.activited
    });
  },
  //事件处理函数
  swiperchange: function (e) {
    //console.log(e.detail.current)
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  toDetailsTap: function (e) {
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
    })
  },
  tapBanner: function (e) {
    // if (e.currentTarget.dataset.id != 0) {
    //   wx.navigateTo({
    //     url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
    //   })
    // }
    wx.showModal({
      title: '提示',
      content: '详细信息请登陆capper官网',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  bindTypeTap: function (e) {
    this.setData({
      selectCurrent: e.index
    })
  },
  onLoad: function () {
    var that = this
    wx.setNavigationBarTitle({
      title: wx.getStorageSync('mallName')
    })
    console.log(1)
    wx.request({
      url: 'https://api.it120.cc/' + app.globalData.subDomain + '/banner/list',
      data: {
        key: 'mallName'
      },
      success: function (res) {
        console.log(2)
        if (res.data.code == 404) {
          wx.showModal({
            title: '提示',
            content: '请在后台添加 banner 轮播图片',
            showCancel: false
          })
        } else {
          var bannerShow = [{
            businessId: "1",
            picUrl:'/images/more/confirm-word11.png'
          }]
          that.setData({
            banners: bannerShow
          });
        }
      }
    }),
      wx.request({
        url: 'https://api.it120.cc/' + app.globalData.subDomain + '/shop/goods/category/all',
        success: function (res) {
          var categories = [{ id: 1, name: "品牌管理" },{ id: 2, name: "内容营销" },{ id: 3, name: "媒介投放" },{ id: 4, name: "创意管理" }];
          // if (res.data.code == 0) {
          //   for (var i = 0; i < res.data.data.length; i++) {
          //     categories.push(res.data.data[i]);
          //   }
          // }
          that.setData({
            categories: categories,
            activeCategoryId: 1
          });
          that.getGoodsList(1);
        }
      })
    that.getCoupons();
    that.getNotice();
  },
  onPageScroll(e) {
    let scrollTop = this.data.scrollTop
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  getGoodsList: function (categoryId) {
    if (categoryId == 0) {
      categoryId = "";
    }
    console.log(categoryId)
    var goods = []
    var data = this.data.brandDes
    for (var i = 0; i < data.length; i++) {
      if (data[i].id == categoryId ) {
        goods.push(data[i]);

      }
    }
    console.log(goods)
    this.setData({goods:goods})
    // wx.request({
    //   url: 'https://api.it120.cc/' + app.globalData.subDomain + '/shop/goods/list',
    //   data: {
    //     categoryId: categoryId,
    //     nameLike: that.data.searchInput
    //   },
    //   success: function (res) {
    //     that.setData({
    //       goods: [],
    //       loadingMoreHidden: true
    //     });
    //     var goods = [];
    //     if (res.data.code != 0 || res.data.data.length == 0) {
    //       that.setData({
    //         loadingMoreHidden: false,
    //       });
    //       return;
    //     }
    //     for (var i = 0; i < res.data.data.length; i++) {
    //       goods.push(res.data.data[i]);
    //     }
    //     that.setData({
    //       goods: goods,
    //     });
    //   }
    // })
  },
  getCoupons: function () {
    var that = this;
    wx.request({
      url: 'https://api.it120.cc/' + app.globalData.subDomain + '/discounts/coupons',
      data: {
        type: ''
      },
      success: function (res) {
        if (res.data.code == 0) {
          that.setData({
            hasNoCoupons: false,
            coupons: res.data.data
          });
        }
      }
    })
  },
  gitCoupon: function (e) {
    var that = this;
    wx.request({
      url: 'https://api.it120.cc/' + app.globalData.subDomain + '/discounts/fetch',
      data: {
        id: e.currentTarget.dataset.id,
        token: wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.code == 20001 || res.data.code == 20002) {
          wx.showModal({
            title: '错误',
            content: '来晚了',
            showCancel: false
          })
          return;
        }
        if (res.data.code == 20003) {
          wx.showModal({
            title: '错误',
            content: '你领过了，别贪心哦~',
            showCancel: false
          })
          return;
        }
        if (res.data.code == 30001) {
          wx.showModal({
            title: '错误',
            content: '您的积分不足',
            showCancel: false
          })
          return;
        }
        if (res.data.code == 20004) {
          wx.showModal({
            title: '错误',
            content: '已过期~',
            showCancel: false
          })
          return;
        }
        if (res.data.code == 0) {
          wx.showToast({
            title: '领取成功，赶紧去下单吧~',
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showModal({
            title: '错误',
            content: res.data.msg,
            showCancel: false
          })
        }
      }
    })
  },
  onShareAppMessage: function () {
    return {
      title: wx.getStorageSync('mallName') + '——' + app.globalData.shareProfile,
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  getNotice: function () {
    var that = this;
    wx.request({
      url: 'https://api.it120.cc/' + app.globalData.subDomain + '/notice/list',
      data: { pageSize: 5 },
      success: function (res) {
        if (res.data.code == 0) {
          that.setData({
            noticeList: res.data.data
          });
        }
      }
    })
  },
  listenerSearchInput: function (e) {
    this.setData({
      searchInput: e.detail.value
    })

  },
  toSearch: function () {
    this.getGoodsList(this.data.activeCategoryId);
  }
})
